package main

import (
	"fmt"
	"github.com/gin-gonic/gin/binding"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"mxshop-api/global"
	"mxshop-api/initialize"
	myvalidator "mxshop-api/validator"
)

func main() {
	port := 8080
	//1.初始化logger
	initialize.InitLogger()
	//2.初始化配置
	initialize.InitConfig()
	zap.S().Infof("启动服务器，端口:%d", global.ServerConfig.Port)
	//3.初始化路由
	engine := initialize.Routers()
	//4.初始化翻译
	initialize.InitTrans("zh")
	//注册验证器
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		v.RegisterValidation("mymobile", myvalidator.ValidateMobile)
		v.RegisterTranslation("mymobile", global.Trans, func(ut ut.Translator) error {
			return ut.Add("mymobile", "{0} 非法的手机号码", true)
		}, func(ut ut.Translator, fe validator.FieldError) string {
			t, _ := ut.T("mymobile", fe.Field())
			return t
		})
	}
	if err := engine.Run(fmt.Sprintf(":%d", port)); err != nil {
		zap.S().Panic("启动失败：", err.Error())
	}
}
